import GameData_red_blue_man from "../Script/GameData_red_blue_man";

const {ccclass, property} = cc._decorator;

export enum musicName {
    bgm = "bgm",
    bounce = "bounce",
    bridge_sound = "bridge_sound",
    button_sound_game = "button_sound_game",
    click = "click",
    Diamond_Sound = "Diamond_Sound",
    Die = "Die",
    FireJump = "FireJump",
    hair_drier_sound = "hair_drier_sound",
    IceJump = "IceJump",
    key_sound = "key_sound",
    Mechanical_sound = "Mechanical_sound",
    PlayerSwap = "PlayerSwap",
    Remote_sensing_sound = "Remote_sensing_sound",
    resuce_people_sound = "resuce_people_sound",
    stone_door = "stone_door",
    stone_Sound = "stone_sound",
    water = "water",
    Win = "Win",
    Yeah_fire = "Yeah_fire",
    Yeah_ice = "Yeah_ice",
    chuansongme = "chuansongme",
    daodiaoluo = "daodiaoluo",
    dongjie = "dongjie",
    qiaoqiaoban = "qiaoqiaoban",
    jiguang = "jiguang",
    tuigreen = "tuigreen",
    zhuanpan = "zhuanpan",
    daoju = "daoju",
}

@ccclass
export default class Music{

    static effectList = ["bgm","bounce","bridge_sound","button_sound_game","click","Diamond_Sound","Die","FireJump","hair_drier_sound","IceJump","key_sound","Mechanical_sound","PlayerSwap","Remote_sensing_sound",
        "resuce_people_sound","stone_door","stone_sound","water","Win","Yeah_fire","Yeah_ice", "chuansongme", "daodiaoluo", "dongjie", "qiaoqiaoban", "jiguang","tuigreen", "zhuanpan", "daoju"];

    static audioClipList = {};

    static loadIndext = 0;

    static musicVolume = 0.2;
    static effectVolume = 1;

    // static musicState = true;
    // static effectState = true;

    //保存正在播放的音乐，好做后续的系列处理
    static playingMus = {};

    static loadAudioClip(file){
        cc.loader.loadRes("Sound/" + file, cc.AudioClip, (err,clip)=>{
            this.audioClipList[file] = clip;
            this.loadIndext ++;
            if(this.effectList[this.loadIndext]) {
                this.loadAudioClip(this.effectList[this.loadIndext]);
            }
        })
    }

    static GamePlayMusic(clip){
        if(!GameData_red_blue_man.settingState.music){
            return;
        }
        this.playBgm(clip);
    }

    static playBgm(clip){
        if(this.audioClipList[clip]){
            cc.audioEngine.playMusic(this.audioClipList[clip],true);
        }else{
            cc.loader.loadRes("Sound/" + clip, cc.AudioClip, (err,res)=>{
                cc.audioEngine.playMusic(res,true);
            });
        }
        cc.audioEngine.setMusicVolume(this.musicVolume);
    }


    //播放音效
    static GamePlayEffect(clip){
        if(!GameData_red_blue_man.settingState.effect){
            return;
        }
        if(this.audioClipList[clip]){
            cc.audioEngine.playEffect(this.audioClipList[clip],false);
        }else{
            cc.loader.loadRes("Sound/" + clip, cc.AudioClip, (err,res)=>{
               cc.audioEngine.playEffect(res,false);
            });
        }
        cc.audioEngine.setEffectsVolume(this.effectVolume);
    }

    //停止指定的音效
    static StopEffectIndex(clip){
        cc.audioEngine.stopEffect(this.playingMus[clip]);
        this.playingMus[clip] = null;
    }

    //暂停_恢复音乐
    static gameMusicControl(type){
        if(type){
            // cc.audioEngine.resumeMusic();
            Music.playBgm(musicName.bgm);
        }else{
            cc.audioEngine.stopMusic();
        }
    }

    static specialEffect = {};
    //播放水流声音
    static specialMusic(data){
        if(!GameData_red_blue_man.settingState.effect || Music.specialEffect[data.name]){
            return;
        }
        Music.specialEffect[data.name] = cc.audioEngine.playEffect(this.audioClipList[data.name],data.loop);
    }

    static stopSpecialMusic(data){
        if(!Music.specialEffect[data.name]){
            return;
        }
        cc.audioEngine.stopEffect(Music.specialEffect[data.name]);
        Music.specialEffect[data.name] = null;
    }
}

